﻿// <copyright file="SquareView.cs" company="Ecole Normale Supérieure">
// Released under GPLv2
// </copyright>
// <summary>Contains the SquareView class.</summary>

namespace Sqwarea.GameLogic.View
{
    using System.Drawing;
    using Sqwarea.GameLogic.Extensions;
    using Sqwarea.Persistence;

    /// <summary>
    /// Class for a view of a square.
    /// Used by UserView to provide just needed information to GUI.
    /// </summary>
    public class SquareView
    {
        /// <summary>
        /// Initializes a new instance of the SquareView class.
        /// Need informations about the king of the square.
        /// </summary>
        /// <param name="square">Square used to initiliaze this class</param>
        /// <param name="preciseTroops">Precise troops number or not</param>
        /// <param name="preciseHasKing">Precise HasKing propoerty or not</param>
        /// <param name="kingsCache">A cache for kings</param>
        public SquareView(Square square, bool preciseTroops, bool preciseHasKing, IKingsCache kingsCache)
        {
            // Get the real King Id (=0 is neutral)
            this.KingId = square.KingId;
            var king = kingsCache.GetKingById(KingId);
            king.UpdateNeutralId();
            this.KingId = king.Id;

            this.Nickname = king.IsNeutral() ? null : king.Nickname;
            this.Position = square.Position;

            if (preciseTroops)
            {
                if (square.HasKing)
                {
                    this.Troops = square.Troops + king.GetAcademyTroops();
                }
                else
                {
                    this.Troops = square.Troops;
                }
            }
            else
            {
                this.Troops = null;
            }

            if (preciseHasKing)
            {
                this.HasKing = square.HasKing;
            }
            else
            {
                this.HasKing = null;
            }
        }

        /// <summary>
        /// Gets king's id.
        /// </summary>
        public long KingId { get; private set;  }

        /// <summary>
        /// Gets the coordinates of this square.
        /// </summary>
        public Point Position { get; private set; }

        /// <summary>
        /// Gets the number of troops in this square.
        /// WARNING : contrary to Persistence.Square, this numbers contains AcademyTroops.
        /// </summary>
        public int? Troops { get; private set; }

        /// <summary>
        /// Gets a value indicating whether this square contains a king.
        /// </summary>
        public bool? HasKing { get; private set; }

        /// <summary>
        /// Gets the nickname of the owner of the square.
        /// </summary>
        public string Nickname { get; private set; }
    }
}
